#include "read_conf.h"

CAllConfigure::CAllConfigure()
{
	cConfFilePath = SERVER_CONF_PATH;
	if (!loadConf())
	{
		ERROR_MSG("load configuration file failed! ");
		bAllConfLoadedRight = false;

		return;
	}

	bAllConfLoadedRight = true;
}

CAllConfigure::~CAllConfigure()
{
	bAllConfLoadedRight = false;
}

bool CAllConfigure::hasLoadConfRight()
{
	return bAllConfLoadedRight;
}

const stServerInfo CAllConfigure::GetLocalServerConfiguration()
{
	return m_LocalServerInfo;
}

const stSmtpInfo CAllConfigure::GetSmtpServerConfiguration()
{
	return m_SMTPServerInfo;
}

const stMysqlInfo CAllConfigure::GetMysqlServerConfiguration()
{
	return m_MysqlServerInfo; 
}

const stFastDfsInfo CAllConfigure::GetFastDfsConfiguration()
{
	return m_FastDfsInfo;
}

bool CAllConfigure::loadConf()
{
	if (!LoadServerInfo(cConfFilePath)) 
	{
		ERROR_MSG("load local server configuration failed! ");
		return false;
	}

	if (!LoadSmtpServerInfo(cConfFilePath))
	{
		ERROR_MSG("load SMTP server configuration failed! ");
		return false;
	}

	if (!LoadMysqlInfo(cConfFilePath))
	{
		ERROR_MSG("load Mysql server configuration failed! ");
		return false;
	}

	if (!LoadFastDfsInfo(cConfFilePath))
	{
		ERROR_MSG("load fastdfs info configuration failed! ");
		return false;
	}

	return true;
}

bool CAllConfigure::LoadServerInfo(const std::string& fileDir)
{
	TiXmlDocument doc;
	if(!doc.LoadFile(fileDir.c_str()))
	{
		ERROR_MSG("load file: " << fileDir.c_str() << " error " << doc.ErrorDesc());	
		return false;
	}

	TiXmlElement* server = doc.FirstChildElement();
	if (server == nullptr)
	{
		ERROR_MSG("can not find root in xml!");
		return false;
	}

	TiXmlElement* m_serverInfo = server->FirstChildElement("localinfo");
	if (m_serverInfo == nullptr)
	{
		ERROR_MSG("can not find local server info xml attribute!");
		return false;
	}

	for(TiXmlElement* m_serverInfoValue = m_serverInfo->FirstChildElement("value");
		m_serverInfoValue != nullptr; m_serverInfoValue = m_serverInfoValue->NextSiblingElement())
	{
		const char* type = m_serverInfoValue->Attribute("type");
		const char* value = m_serverInfoValue->Attribute("value");

		if (strcmp(type, "serverIp") == 0)
		{
			m_LocalServerInfo.serverIp = value;
		}
		else if (strcmp(type, "port") == 0)
		{
			m_LocalServerInfo.port = atoi(value);
		}
		else if (strcmp(type, "backlog") == 0)
		{
			m_LocalServerInfo.backlog = atoi(value);
		}
		else if (strcmp(type, "maxevents") == 0)
		{
			m_LocalServerInfo.maxevents = atoi(value);
		}
		else
		{
			INFO_MSG("server info conf type: " << type << "value: " <<  value);
			return false;
		}
	}

	doc.Clear();

	return true;
}

bool CAllConfigure::LoadSmtpServerInfo(const std::string& fileDir)
{
	TiXmlDocument doc;
	if(!doc.LoadFile(fileDir.c_str()))
	{
		ERROR_MSG("load file: " << fileDir.c_str() << " error " << doc.ErrorDesc());	
		return false;
	}

	TiXmlElement* server = doc.FirstChildElement();
	if (server == nullptr)
	{
		ERROR_MSG("can not find root in xml!");
		return false;
	}

	TiXmlElement* m_SmtpInfo = server->FirstChildElement("smtpinfo");
	if (m_SmtpInfo == nullptr)
	{
		ERROR_MSG("can not find root in smtpinfo xml!");
		return false;
	}

	for(TiXmlElement* m_SmtpInfoValue = m_SmtpInfo->FirstChildElement("value");
		m_SmtpInfoValue != nullptr; m_SmtpInfoValue = m_SmtpInfoValue->NextSiblingElement())
	{
		const char* type = m_SmtpInfoValue->Attribute("type");
		const char* value = m_SmtpInfoValue->Attribute("value");

		if (strcmp(type, "SmtpServerAddr") == 0)
		{
			m_SMTPServerInfo.cSmtpServer = value;
		}
		else if (strcmp(type, "SmtpServerPort") == 0)
		{
			m_SMTPServerInfo.uiPort = atoi(value);
		}
		else if (strcmp(type, "EmailSenderUserName") == 0)
		{
			m_SMTPServerInfo.cUsername = value;
		}
		else if (strcmp(type, "EmailSenderPassWord") == 0)
		{
			m_SMTPServerInfo.cPassword = value;
		}
		else
		{
			ERROR_MSG("server info conf type: " << type << ", value: " <<  value);
			return false;
		}
	}

	doc.Clear();

	return true;
}

bool CAllConfigure::LoadMysqlInfo(const std::string& fileDir)
{
	TiXmlDocument doc;
	if(!doc.LoadFile(fileDir.c_str()))
	{
		ERROR_MSG("load file: " << fileDir.c_str() << "error " << doc.ErrorDesc());	
		return false;
	}

	TiXmlElement* server = doc.FirstChildElement();
	if (server == nullptr)
	{
		ERROR_MSG("can not find root in xml!");
		return false;
	}

	TiXmlElement* m_mysqlInfo = server->FirstChildElement("mysqlinfo");
	if (m_mysqlInfo == nullptr)
	{
		ERROR_MSG("can not find m_mysqlInfo in xml!");
	}

	for(TiXmlElement* m_mysqlInfoValue = m_mysqlInfo->FirstChildElement("value");
		m_mysqlInfoValue != nullptr; m_mysqlInfoValue = m_mysqlInfoValue->NextSiblingElement())
	{
		const char* type = m_mysqlInfoValue->Attribute("type");
		const char* value = m_mysqlInfoValue->Attribute("value");

		if (strcmp(type, "mysqlAddr") == 0)
		{
			m_MysqlServerInfo.cMysqlAddr = value;
		}
		else if (strcmp(type, "mysqlPort") == 0)
		{
			m_MysqlServerInfo.uiPort = atoi(value);
		}
		else if (strcmp(type, "database") == 0)
		{
			m_MysqlServerInfo.cDatabase = value;
		}
		else if (strcmp(type, "username") == 0)
		{
			m_MysqlServerInfo.cUsername = value;
		}
		else if (strcmp(type, "password") == 0)
		{
			m_MysqlServerInfo.cPassword = value;
		}
		else
		{
			ERROR_MSG("mysqlinfo conf type: " << type << ", value: " <<  value);
			return false;
		}
	}

	doc.Clear();

	return true;
}

bool CAllConfigure::LoadFastDfsInfo(const std::string& fileDir)
{
	TiXmlDocument doc;
	if(!doc.LoadFile(fileDir.c_str()))
	{
		ERROR_MSG("load file: " << fileDir.c_str() << " error " << doc.ErrorDesc());	
		return false;
	}

	TiXmlElement* server = doc.FirstChildElement();
	if (server == nullptr)
	{
		ERROR_MSG("can not find root in xml!");
		return false;
	}

	TiXmlElement* m_fastDfsInfo = server->FirstChildElement("fastdfsinfo");
	if (m_fastDfsInfo == nullptr)
	{
		ERROR_MSG("can not find root in fastdfsinfo xml!");
		return false;
	}

	for(TiXmlElement* m_FastDfsInfoValue = m_fastDfsInfo->FirstChildElement("value");
		m_FastDfsInfoValue != nullptr; m_FastDfsInfoValue = m_FastDfsInfoValue->NextSiblingElement())
	{
		const char* type = m_FastDfsInfoValue->Attribute("type");
		const char* value = m_FastDfsInfoValue->Attribute("value");

		if (strcmp(type, "clientcfg") == 0)
		{
			m_FastDfsInfo.cClientCfg = value;
		}
		else if (strcmp(type, "domain") == 0)
		{
			m_FastDfsInfo.cDomain = value;
		}
		else
		{
			ERROR_MSG("server info conf type: " << type << ", value: " <<  value);
			return false;
		}
	}

	doc.Clear();

	return true;
}
